﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework;
using OLTS.DAL;
using OLTS.MODEL;

namespace OLTS.DATA_SERVICE
{
    public class Project_Service
    {
        private OTS_PROJECT _objApp = new OTS_PROJECT();

        public Project_Service(OTS_PROJECT project)
        {
            _objApp = project;
        }
        public Project_Service()
        {

        }
        public OTS_PROJECT GetByID(string employee_id)
        {
            using (var ctx = new Entities())
            {
                var res = new OTS_PROJECT();
                var obj = ctx.OTS_ASSIGN_PROJECT.Include("OTS_PROJECT").Where(o => o.EMPLOYEE_ID == employee_id).FirstOrDefault();

                if (obj != null)
                {
                    res = ctx.OTS_PROJECT.Include("OTS_SITE").Where(x => x.ID == obj.PROJECT_ID).FirstOrDefault();
                }
                return res;
            }

        }
        public List<OTS_PROJECT> GetProjectNameAllById(int site_id)
        {
            using (var ctx = new Entities())
            {
                var res = new OTS_PROJECT();
                var obj = ctx.OTS_PROJECT.Include("OTS_SITE").Include("OTS_ASSIGN_PROJECT").Where(o => o.SITE_ID == site_id && o.STATUS == true).ToList();

                return obj;
            }

        }
        public List<OTS_PROJECT> GetAllinclude()
        {
            var ctx = new Entities();
            var obj = ctx.OTS_PROJECT.Include("OTS_SITE").Include("OTS_ASSIGN_PROJECT").Where(x => x.STATUS == true && x.OTS_SITE.STATUS == true).ToList();
            return obj;
        }

        public void Edit()
        {
            try
            {
                var ctx = new Entities();
                var obj = ctx.OTS_PROJECT.Where(x => x.ID == _objApp.ID).FirstOrDefault();
                if (obj != null)
                {
                    obj.NAME = _objApp.NAME;
                    obj.SITE_ID = _objApp.SITE_ID;
                    obj.START_DATE = _objApp.START_DATE;
                    obj.END_DATE = _objApp.END_DATE;
                    obj.STATUS = _objApp.STATUS;
                    obj.CREATED = DateTime.Now;
                    obj.CREATE_BY = _objApp.CREATE_BY;
                }

                ctx.SaveChanges();
            }
            catch (Exception e)
            {
                
                throw e;
            }
           
        }

        public void Add()
        {
            try
            {
                var ctx = new Entities();
                _objApp.CREATED = DateTime.Now;
                ctx.OTS_PROJECT.AddObject(_objApp);
                ctx.SaveChanges();
            }
            catch (Exception e)
            {

                throw e;
            }

        }

        public void Delete()
        {
            try
            {
                var ctx = new Entities();
                var obj = ctx.OTS_PROJECT.Where(x => x.ID == _objApp.ID).FirstOrDefault();
                if (obj != null)
                {
                    obj.STATUS = false;
                    obj.CREATED = DateTime.Now;
                }
                // ctx.OTS_PROJECT.DeleteObject(obj);
                ctx.SaveChanges();
            }
            catch (Exception e)
            {

                throw e;
            }

        }

    }
}
